/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var addTwoNumbers = function (l1, l2) {
  let dummy = new ListNode()
  let curr = dummy
  let carry = 0

  while (l1 !== null || l2 !== null) {
    let sum = 0
    if (l1 !== null) {
      sum += l1.val
      l1 = l1.next
    }
    if (l2 !== null) {
      sum += l2.val
      l2 = l2.next
    }

    sum += carry
    curr.next = new ListNode(sum % 10) //贴新节点，值为模
    carry = Math.floor(sum / 10) //下一个节点是否进位，进位多少
    // 把curr往前挪一位，移动next
    curr = curr.next //准备好贴下一个新节点
  }
  if (carry > 0) {
    curr.next = new ListNode(carry)
  }
  return dummy.next
};